package uz.rt.dicom.data;

import uz.rt.dicom.data.element.UnsignedLong;
import uz.rt.dicom.io.DicomIOException;
import uz.rt.dicom.io.DicomInputStream;
import uz.rt.dicom.uid.TransferSyntax;

import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MetaInformation
{
// ------------------------------ FIELDS ------------------------------

    private static final int lengthPreamble = 128;//preamble 128 bytes om te skippen
    private static final String prefix = "DCIM";
    private static final Logger logger = Logger.getLogger(MetaInformation.class.getName());
    ArrayList<IDataElement> metaList = new ArrayList<IDataElement>();
    private long preamble;
    private TransferSyntax headerSyntax;
    private UnsignedLong metaElement;
    private long metaHeaderLength;

// --------------------------- CONSTRUCTORS ---------------------------

    public MetaInformation()
    {
        this.headerSyntax = TransferSyntax.ExplicitVRLittleEndian;
    }

// --------------------- GETTER / SETTER METHODS ---------------------

    public TransferSyntax getHeaderSyntax()
    {
        return headerSyntax;
    }

    public UnsignedLong getMetaElement()
    {
        return metaElement;
    }

    public long getMetaHeaderLength()
    {
        return metaHeaderLength;
    }

// -------------------------- OTHER METHODS --------------------------

    public void clear()
    {
        metaList.clear();
    }

    /**
     * Read the meta information from a dicom stream and stores its data elements. If no meta information can be
     * detected -1 is returned.
     *
     * @param dicomInputStream Dicom input stream
     * @return 0 if meta information was detected, -1 if no meta informat
     * @throws IOException If an IO error occurs.
     */
    public int read(DicomInputStream dicomInputStream) throws IOException
    {
        if (dicomInputStream.skip(lengthPreamble) != lengthPreamble)
            throw new IOException("Unable to skip preamble on dicom input stream.");
        String s = dicomInputStream.readString(4, "ISO-8859-1");
        if (!s.equals(prefix))
        {
            logger.log(Level.WARNING,
                       "Dicom preamble and prefix doesn't seem to be present in the dicom input stream.");
            return -1;
        } else
        {
            metaElement = (UnsignedLong) dicomInputStream.readElement();
            metaHeaderLength = (metaElement.getValue()).get(0);
            int remaining = (int) metaHeaderLength;
            int pos;
            while (remaining > 0)
            {
                pos = dicomInputStream.getPosition();
                metaList.add(dicomInputStream.readElement());
                remaining -= dicomInputStream.getPosition() - pos;
            }
            if (remaining < 0)
                throw new DicomIOException("Remaining metaHeader length should equal zero at the end of " +
                                                   "the header, but found a remaining length that is lower then zero.");
        }
        return 0;
    }

    /**

     dicom prefix 4 bytes string DCIM checken anders geen dicomfile!
     Explicit VR Little Endian Transfer Syntax voor header!
     File Meta Information Group Length(0002,0000)
     File Meta Information Version (0002,0001)This is a two byte field where each bit identifies a version of
     this File Meta Information header.
     Media Storage SOP Class UID (0002,0002)
     Media Storage SOP Instance UID (0002,0003)
     Transfer Syntax UID (0002,0010)
     Implementation Class UID (0002,0012)
     Implementation Version Name (0002,0013)
     Source Application Entity Title (0002,0016)
     Private Information Creator UID (0002,0100)
     Private Information (0002,0102)
     */
}
